<template>
  <div class="wrap">
    <com-recommend
      :errorWatch="true"
      :houDelRecommendVO="houDelRecommendVO"
      :houDelRecommendErrorMsg="houDelRecommendErrorMsg"
      :houDelVO="info.houDelVO"
    ></com-recommend>
  </div>
</template>
<script>
import ComRecommend from '@/views/house/com/com-recommend.vue';
import { mapGetters, mapState } from 'vuex';
import { houseRecommend } from '@/config/house/houseDetailConfig';
import { updateRecommendInfo } from '@/api/user';

export default {
  name: 'ComHouseRecommend',
  shulan: true,
  components: {
    ComRecommend,
  },
  props: {},
  data() {
    return {
      modifiedCount: 2,
      houDelRecommendVO: {
        title: '',
        sellingPoint: '', //核心卖点
        ownerPsychology: '', //业主心态
        serviceIntroduction: '', //服务介绍
        surroundMating: '', // 小区配套
        service: '', //税费信息
      },
      // 错误提示
      houDelRecommendErrorMsg: {
        title: '',
        sellingPoint: '', //核心卖点
        ownerPsychology: '', //业主心态
        serviceIntroduction: '', //服务介绍
        surroundMating: '', // 小区配套
        service: '', //税费信息
      },
    };
  },
  created() {
    const { info } = this;
    this.renderRecommend(info.houDelRecommendVO, info.houDelVO.title);
  },
  mounted() {},
  watch: {
    "houDelRecommendVO.title": {
      immediate: true,
      handler(val) {
        // 将title同步
        if (val) {
          this.info.houDelVO.title = val
        }
      }
    },
    "houDelRecommendVO": {
      deep: true,
      handler() {
        this.modifiedCount--
      }
    }
  },
  computed: {
    ...mapState({
      info: (state) => state.houseInfo,
    }),
    ...mapGetters({
      houDelId: 'houseInfo/houDelId',
    }),
  },
  methods: {
    renderRecommend(list, title) {
      this.houDelRecommendVO.title = title;
      if (list) {
        for (let key in this.houDelRecommendVO) {
          if (list[key]) {
            let item = houseRecommend.find((v) => v.key == key);
            this.houDelRecommendVO[key] = list[key].context || (item && item.defaultValue);
          }
        }
      }
    },

    // 保存推荐信息
    save() {
      if (this.modifiedCount > 0) return Promise.resolve()

      // 注意title是保存在基本信息里的
      return updateRecommendInfo({
        houDelRecStr: JSON.stringify({
          deleted: false,
          houDelId: this.houDelId,
          ...this.getRecommendVo(this.houDelRecommendVO, houseRecommend),
        }),
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.wrap{
  height: 500px;
  overflow: auto;
  padding-right: 10px;
  @include scrollBar
}

</style>

